package com.gzz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzz.common.R;
import com.gzz.common.StringIntersection;
import com.gzz.pojo.GoodsInfo;
import com.gzz.service.GoodsInfoService;
import com.gzz.service.OssUploadService;
import com.gzz.vo.GoodsInfoList;
import com.gzz.vo.GoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.gzz.utils.OssUtils.GoodTypeId;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周佳成
 * @since 2023-11-24
 */
@Slf4j
@RestController
@RequestMapping("/api/goods-info")
public class GoodsInfoController {
    @Autowired
    GoodsInfoService goodsInfoService;
    @Autowired
    OssUploadService ossUploadService;
    @PostMapping("/upload")
    public R<String> Scupload(String type,@RequestParam("file") MultipartFile multipartFile){
        String imgUrl = ossUploadService.uploadFile(multipartFile,type);
        if(imgUrl!="error"){
            return R.success(imgUrl);
        }
        return R.error(imgUrl);
    }
    @GetMapping
    public R<List<GoodsInfo>> getAll(@RequestParam(name="pageSize") Integer pageSize,@RequestParam(name="pageNo")Integer pageNo)
    {
        Page<GoodsInfo> goodsInfoPage = new Page<>(pageNo,pageSize);
        goodsInfoService.page(goodsInfoPage);
        List<GoodsInfo> goodsInfoList = goodsInfoPage.getRecords();
        return R.success(goodsInfoList);
    }
    @GetMapping("/search")
    public R<List<GoodsInfo>> getResult(@RequestParam(name="search") String search)
    {
        List<GoodsInfo> goodsInfoList = goodsInfoService.list();
        for(GoodsInfo goodsInfo : goodsInfoList)
        {
            if(!StringIntersection.hasIntersection(goodsInfo.toString(),search))
            {
                goodsInfoList.remove(goodsInfo);
            }
        }
        return R.success(goodsInfoList);
    }
    @GetMapping("/detail")
    public R<GoodsInfo> getDetail(@RequestParam(name = "goodsId") Long goodsId)
    {
          return R.success(goodsInfoService.getById(goodsId));
    }

    @GetMapping("/searchlist")
    public R<GoodsInfoList> getSearchList(@RequestParam(name= "pageNum")Integer pageNum,@RequestParam(name = "pageSize")Integer pageSize,@RequestParam(name="searchName")String searchName,@RequestParam(name = "searchKeyWord")String searchKeyWord,@RequestParam(name="topPrice")Double topPrice,@RequestParam(name = "bottomPrice")Double bottomPrice)
    {
        //searchName = '%'+searchName+'%';
        //searchKeyWord = '%' + searchKeyWord + '%';
        QueryWrapper<GoodsInfo>goodsInfoQueryWrapper = new QueryWrapper<>();
        goodsInfoQueryWrapper.like(searchName!="","goods_Name",searchName);
        goodsInfoQueryWrapper.like(searchKeyWord!="","goods_Desc",searchKeyWord);
        if(topPrice.toString().length()!=0&&bottomPrice.toString().length()!=0&&topPrice>bottomPrice)
        {
            goodsInfoQueryWrapper.between("goods_Price",bottomPrice,topPrice);
        }
        Page<GoodsInfo> goodsInfoPage = new Page<>(pageNum,pageSize);
        goodsInfoService.page(goodsInfoPage,goodsInfoQueryWrapper);
        List<GoodsInfo> goodsInfoList = goodsInfoPage.getRecords();
        List<GoodsVo> goodsVoList = goodsInfoService.getVo(goodsInfoList);
        log.info("结果为："+goodsVoList.toString());
        GoodsInfoList goodsInfoList1 = new GoodsInfoList(goodsVoList,goodsInfoService.list().size());
        return R.success(goodsInfoList1);
    }

    @GetMapping("/reset")
    public R<GoodsInfoList> getReset(@RequestParam(name="pageNum")Integer pageNum,@RequestParam(name="pageSize")Integer pageSize)
    {
        Page<GoodsInfo> goodsInfoPage = new Page<>(pageNum,pageSize);
        goodsInfoService.page(goodsInfoPage);
        List<GoodsInfo> goodsInfoList  = goodsInfoPage.getRecords();
        List<GoodsVo> goodsVoList = goodsInfoService.getVo(goodsInfoList);
        GoodsInfoList goodsInfoList1 = new GoodsInfoList(goodsVoList,goodsInfoService.list().size());
        return R.success(goodsInfoList1);
    }
    @DeleteMapping("/delete")
    public R GoodsDelete(@RequestParam(name = "goodsId") Long goodsId)
    {

        log.info("商品id:"+goodsId);
        goodsInfoService.removeByIdAndTypeId(Long.valueOf(goodsId),GoodTypeId);
        goodsInfoService.removeById(goodsId);
        return R.success(true);
    }

    @PutMapping("/update")
    public R goodsUpdate(@RequestBody GoodsVo goodsVo)
    {
        log.info("结果为"+goodsVo.toString());
        goodsInfoService.removeById(goodsVo.getGoodsId());
        goodsInfoService.saveVo(goodsVo);
        return R.success(true);
    }

    @PostMapping("/addgoods")
    public R goodsAdd(@RequestBody GoodsVo goodsVo)
    {

        goodsInfoService.saveVo(goodsVo);
        return R.success(true);
    }

}

